package com.nuaa.service.change.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuaa.dao.change.inter.ChangeDaoInter;
import com.nuaa.dao.change.inter.ExpDaoInter;
import com.nuaa.dao.change.inter.ProductDaoInter;
import com.nuaa.dao.change.inter.SupplyDaoInter;
import com.nuaa.domain.change.ChangeEntity;
import com.nuaa.domain.change.ExpEntity;
import com.nuaa.domain.change.ProductEntity;
import com.nuaa.domain.change.SupplyEntity;
import com.nuaa.domain.transfer.TransSupEntity;
import com.nuaa.domain.transfer.TransferEntity;
import com.nuaa.formmodel.ChanHandForm;
import com.nuaa.service.change.inter.ChangeServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.BusinessException;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;
/**
 * 换标订单service
 * @author heaven
 *
 */
@Service("changeService")
public class ChangeServiceImpl implements ChangeServiceInter{
	@Resource(name="changeDao")
	private ChangeDaoInter changeDao;
	@Resource(name="proDao")
	private ProductDaoInter proDao;
	@Resource(name="expDao")
	private ExpDaoInter expDao;
	@Resource(name="supplyDao")
	private SupplyDaoInter supplyDao;
	/**保存一条订单信息*/
	@Override
	public AjaxMsg saveOrder(ChangeEntity entity) {
		return changeDao.saveOrder(entity);
	}
	@Transactional
	@Override
	public AjaxMsg saveModOrder(ChangeEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		ChangeEntity chanEntity = changeDao.find(ChangeEntity.class, entity.getId());
		chanEntity.setBox_total(entity.getBox_total());
		chanEntity.setPro_ave_weight(entity.getPro_ave_weight());
		chanEntity.setPro_length(entity.getPro_length());
		chanEntity.setPro_width(entity.getPro_width());
		chanEntity.setPro_height(entity.getPro_height());
		chanEntity.setSku_type_numbers(entity.getSku_type_numbers());
		chanEntity.setPro_total(entity.getPro_total());
		try{
			changeDao.save(chanEntity);
			msg.setSuccess(true);
			msg.setMsg("修改成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("修改失败！");
		}
		return msg;
	}

	/**保存所有产品信息*/
	@Override
	public AjaxMsg saveProducts(List<ProductEntity> entityList){
		return proDao.saveProducts(entityList);
	}
	/**保存所有物流信息*/
	@Override
	public AjaxMsg saveExps(List<ExpEntity> list) {
		return expDao.saveExps(list);
	}
	/**获取当前用户所有订单信息*/
	@Override
	public DataGrid<Map<String, Object>> getChangeOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where 1=1 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=changeDao.count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=changeDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**获取所有用户未审核所有订单*/
	@Override
	public DataGrid<Map<String, Object>> getWaitChangeOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where state = 0 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=changeDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=changeDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	/**获取所有用户已完成所有订单*/
	@Override
	public DataGrid<Map<String, Object>> getFinishChangeOrders(PageInfo pager,String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where state = 6 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=changeDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=changeDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**处理中订单*/
	@Override
	public DataGrid<Map<String, Object>> getProceChangeOrders(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where state > 0 and state < 6 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=changeDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=changeDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**保存一条补充材料信息*/
	@Transactional
	@Override
	public AjaxMsg saveSupply(SupplyEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			//首先判断是否提交过该订单的补充文档
			StringBuilder sql=new StringBuilder();
			sql.append("select * from sup_info where order_no = '");
			sql.append(entity.getOrder_no());
			sql.append("'");
			List<SupplyEntity> list = supplyDao.search(SupplyEntity.class, sql.toString());
			if(list==null || list.size()==0){
				supplyDao.save(entity);
			}else{
				/***
				 */
				SupplyEntity temp = list.get(0);
				if(!entity.getNew_barcode_name().equals("")){
					supplyDao.delFileByName(temp.getNew_barcode_name());
					temp.setNew_barcode_name(entity.getNew_barcode_name());
				}
					
				if(!entity.getNew_old_name().equals("")){
					supplyDao.delFileByName(temp.getNew_old_name());
					temp.setNew_old_name(entity.getNew_old_name());
				}
				if(!entity.getOut_tip_name().equals("")){
					supplyDao.delFileByName(temp.getOut_tip_name());
					temp.setOut_tip_name(entity.getOut_tip_name());
				}
				if(!entity.getRemove_order_name().equals("")){
					supplyDao.delFileByName(temp.getRemove_order_name());
					temp.setRemove_order_name(entity.getRemove_order_name());
				}
				if(!entity.getOther_file().equals("")){
					supplyDao.delFileByName(temp.getOther_file());
					temp.setOther_file(entity.getOther_file());
				}
				if(!entity.getOther_file2().equals("")){
					supplyDao.delFileByName(temp.getOther_file2());
					temp.setOther_file2(entity.getOther_file2());
				}
				if(!entity.getOther_file3().equals("")){
					supplyDao.delFileByName(temp.getOther_file3());
					temp.setOther_file3(entity.getOther_file3());
				}
				if(!entity.getOther_file4().equals("")){
					supplyDao.delFileByName(temp.getOther_file4());
					temp.setOther_file4(entity.getOther_file4());
				}
				supplyDao.update(temp);
			}
			msg.setSuccess(true);
			msg.setMsg("提交成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}
	/**获取所有补充材料信息*/
	@Override
	public DataGrid<Map<String, Object>> getAllSupInfo(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sup_info where 1=1 ");
		try{
			long total=supplyDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=supplyDao.searchForMap(sql.toString(),fm[0],fm[1]," subTm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取信息出错！",e);
		}
	}
	@Override
	public ChangeEntity getById(Integer id) {
		return changeDao.find(ChangeEntity.class, id);
	}
	@Override
	@Transactional
	public void update(ChangeEntity change) {
		
		changeDao.update(change);		
		
	}
	@Override
	public DataGrid<Map<String, Object>> getAllUncheck(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where 1=1 and state = 0 ");
		try{
			long total=changeDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=changeDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public ChangeEntity getByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from change_order where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<ChangeEntity> list = changeDao.search(ChangeEntity.class, sql.toString());
		return list.get(0);
	}
	@Override
	public SupplyEntity getSupByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sup_info where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<SupplyEntity> list = supplyDao.search(SupplyEntity.class, sql.toString());
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	/**
	 * 订单操作提交
	 */
	@Transactional
	@Override
	public AjaxMsg saveHand(String id, ChanHandForm form) {
		AjaxMsg msg = new AjaxMsg();
		try{
				ChangeEntity entity = changeDao.find(ChangeEntity.class,Integer.parseInt(id));
				entity.setState(form.getState());
				entity.setLaborFee(form.getLaborFee());
				entity.setPostage(form.getPostage());
				
				if(form.getArr_tab_name()!=null &&!form.getArr_tab_name().equals("")){
					supplyDao.delFileByName(entity.getArr_tab_name());
					entity.setArr_tab_name(form.getArr_tab_name());
				}
				if(form.getChan_fee_tab_name()!=null && !form.getChan_fee_tab_name().equals("")){
					supplyDao.delFileByName(entity.getChan_fee_tab_name());
					entity.setChan_fee_tab_name(form.getChan_fee_tab_name());
				}
				if(form.getChan_fini_number_name()!=null && !form.getChan_fini_number_name().equals("")){
					supplyDao.delFileByName(entity.getChan_fini_number_name());
					entity.setChan_fini_number_name(form.getChan_fini_number_name());
				}
				if(form.getChan_model_name()!=null && !form.getChan_model_name().equals("")){
					supplyDao.delFileByName(entity.getChan_model_name());
					entity.setChan_model_name(form.getChan_model_name());
				}
				if(form.getLogis_tab_name()!=null && !form.getLogis_tab_name().equals("")){
					supplyDao.delFileByName(entity.getLogis_tab_name());
					entity.setLogis_tab_name(form.getLogis_tab_name());
				}
				if(form.getPack_tab_name()!=null && !form.getPack_tab_name().equals("")){
					supplyDao.delFileByName(entity.getPack_tab_name());
					entity.setPack_tab_name(form.getPack_tab_name());
				}
				if(form.getRec_tab_name()!=null && !form.getRec_tab_name().equals("")){
					supplyDao.delFileByName(entity.getRec_tab_name());
					entity.setRec_tab_name(form.getRec_tab_name());
				}
				if(form.getOther_file1()!=null && !form.getOther_file1().equals("")){
					supplyDao.delFileByName(entity.getOther_file1());
					entity.setOther_file1(form.getOther_file1());
				}
				if(form.getOther_file2()!=null&&!form.getOther_file2().equals("")){
					supplyDao.delFileByName(entity.getOther_file2());
					entity.setOther_file2(form.getOther_file2());
				}
				if(form.getOther_file3()!=null&&!form.getOther_file3().equals("")){
					supplyDao.delFileByName(entity.getOther_file3());
					entity.setOther_file3(form.getOther_file3());
				}
				if(form.getOther_file4()!=null&&!form.getOther_file4().equals("")){
					supplyDao.delFileByName(entity.getOther_file4());
					entity.setOther_file4(form.getOther_file4());
				}
				changeDao.update(entity);
			msg.setSuccess(true);
			msg.setMsg("操作成功！");
		}catch (Exception e) {
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("操作失败！");
		}
		return msg;
	}
}
